$(function(){
    var editor = new baidu.editor.ui.Editor(),
    editor_en = new baidu.editor.ui.Editor();
    editor.render('content');
    editor_en.render('content_en');
    
    //form -start
    var uploadifySetData = function(){
        $('#photo-uploader').uploadifySettings('scriptData', {
            'album':$('input[name=album]').val(),
            'thumb_width': $('input[name=thumb_width]').val(),
            'thumb_height': $('input[name=thumb_height]').val(),
            'user':USER_ID,
            'albumName': 'temp_name_entry',
            'albumDescription': 'entry'
        });
    };
    
    $('#photo-uploader').uploadify({
        //'buttonImg': Z.url('/assets/images/select_button_bg.png'),
        'uploader'  : Z.url('/assets/js/uploadify/uploadify.swf'),
        'script'    :  Z.url('/assets/js/uploadify/photo_upload.php'),
        'cancelImg' : Z.url('/assets/js/uploadify/cancel.png'),
        'folder'    : '/assets/uploads',
        'fileDataName': 'photo_upload',
        'fileExt'     : '*.jpg;*.gif;*.png;*.bmp;*.jpeg;*.JPG;*.GIF;*.PNG;*.BMP;*.JPEG',
        'sizeLimit'   : 2 * 1024 * 1024,
        'queueSizeLimit': 10,
        'multi' :  true,
        'auto'  : false,
        'onSelectOnce' : function(){
            uploadifySetData();
        },
        'onComplete' : function(event,id,fileObject,response,data){
            Z.log(response);
            var responseJson;
            eval('responseJson = '+response+';');
            $('input[name=album]').val(responseJson.data.album);
            uploadifySetData();
        },
        'onError': function(event,id,fileObject,errorObject){
            Z.log(event,id,fileObject,errorObject);
        }
    });
    
    $('#photo-uploader-button').click(function(){
        $('#photo-uploader').uploadifyUpload();
        return false;
    });
    
    $('#photo-uploader-clean').click(function(){
        return false;
    });
    //form -end
    
    Z.tab($('ul.tab-button a'), $('.tab-content'), ['active','']);
    
    $('form').submit(function(){
        Z.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            data: $(this).serialize(),
            success: function(data){
                editor.sync();
                editor_en.sync();
                window.location.href=Z.url('/backend/entry/show/'+data.id);
            }
        });
        return false;
    });
});